//
//  TransitionManager.swift
//  UI
//
//  Created by admin on 14/9/22.
//  Copyright (c) 2014年 sinosun. All rights reserved.
//

import UIKit
let π : CGFloat = 3.14159265359
class TransitionManager : NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
    private var presenting = true
    //MARK: - UIViewControllerAnimatedTransitioning
    // This is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
    // synchronize with the main animation.
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval{
        return 0.75
    }
    
    // This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        //在这里定制动画
        let container = transitionContext.containerView()
        println("container=\(container)")
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        println("fromView=\(fromView)")
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        println("toView=\(toView)")
        
        
         // set up from 2D transforms that we'll use in the animation
        //代表旋转的弧度
        let offScreenRight : CGAffineTransform = CGAffineTransformMakeRotation(-π/2)
        //Affine[fangshe]Rotation[旋转]
        let offScreenLeft = CGAffineTransformMakeRotation(π/2)
        
        
        // prepare the toView for the animation
        //如果是显示，视图从右向左；如果是隐藏，视图从左向右；
        toView.transform = self.presenting ? offScreenRight : offScreenLeft
        
        // set the anchor point so that rotations happen from the top-left corner
        //anchorPiont 锚点
        toView.layer.anchorPoint = CGPoint(x:0, y:0)
        fromView.layer.anchorPoint = CGPoint(x:0, y:0)
        // updating the anchor point also moves the position to we have to move the center position to the top-left to compensate
        toView.layer.position = CGPoint(x:0, y:0)
        fromView.layer.position = CGPoint(x:0, y:0)
        
        // add the both views to our view controller
        container.addSubview(toView)
        container.addSubview(fromView)
        
        // get the duration of the animation
        // DON'T just type '0.5s' -- the reason why won't make sense until the next post
        // but for now it's important to just follow this approach
        let duration = self.transitionDuration(transitionContext)
        
        UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: nil, animations: {
            
            // slide fromView off either the left or right edge of the screen
            // depending if we're presenting or dismissing this view
            fromView.transform = self.presenting ? offScreenLeft : offScreenRight
            toView.transform = CGAffineTransformIdentity
            
            }, completion: { finished in
                
                // tell our transitionContext object that we've finished animating
                transitionContext.completeTransition(true)
                
        })
    }
    
    // This is a convenience and if implemented will be invoked by the system when the transition context's completeTransition: method is invoked.
    func animationEnded(transitionCompleted: Bool){
        println("animationEnded")
    }
    
    
    
    //MARK: -UIViewControllerTransitioningDelegate
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning{
        self.presenting = true
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        self.presenting = false
        return self
    }

}
